﻿using AutoMapper;
using ClientBasicData.Api.Application.Command;
using ClientBasicData.Api.DTOS;
using ClientBasicData.Domain.Client;
using ClientBasicData.ErrorCode;
using ClientBasicData.Infrastructure.Interface;
using MediatR;

namespace ClientBasicData.Api.Application.Handler
{
    /// <summary>
    /// 获取客户信息处理类
    /// </summary>
    public class GetClientHandler : IRequestHandler<GetClientCommand, ApiResult<List<ClientDto>>>
    {
        private readonly IBaseRespository<ClientModel> clientRepo;

        public GetClientHandler(IBaseRespository<ClientModel> clientRepo)
        {
            this.clientRepo = clientRepo;
        }

        public Task<ApiResult<List<ClientDto>>> Handle(GetClientCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ClientDto>> res =new ApiResult<List<ClientDto>>();
            var clients = clientRepo.GetAll().Where(x => !x.Isdel);

            var linq = from a in clients
                       select new ClientDto
                       {
                           Id = a.Id,
                           Clientaddr = a.Clientaddr,
                           Clientendtime = a.Clientendtime,
                           Clientname = a.Clientname,
                           Clientnum = a.Clientnum,
                           Clientstarttime = a.Clientstarttime,
                           Clienttel = a.Clienttel,
                           States = a.States,
                           Typeid = a.Typeid,
                           CreateName=a.CreateName,
                           CreateTime = a.CreateTime,
                       };
            res.Data=linq.ToList();
            res.Code=ApiEnum.Success;
            res.Msg = "查询成功";
            return Task.FromResult(res);
        }
    }
}
